<template>
  <div class="globalHeader">
    <a-row :wrap="false">
      <!--  -->
  <a-col flex="200px">
    <RouterLink to="/">
      <div class="title-bar">
        <img class="logo" src="../assets/logo.jpg" alt="logo" />
        <div class="title">云图库</div>
      </div>
    </RouterLink>
  </a-col>
  <!--  -->
  <a-col flex="auto">
    <a-menu
      v-model:selectedKeys="current"
      mode="horizontal"
      :items="items"
      @click="doMenuClick"
    />
  </a-col>

  <!--  -->
  <div class="user-login-status">
  <!-- 退出模块 -->
<div v-if="loginUserStore.loginUser.id">
  <a-dropdown>
    <ASpace>
      <a-avatar style="background-color: #87d068" shape="square" size="large" :src="loginUserStore.loginUser.userAvatar">
        <template #icon><UserOutlined /></template>
      </a-avatar>
      {{ loginUserStore.loginUser.userName ?? '无名' }}
    </ASpace>
    <template #overlay>
      <a-menu>
        <a-menu-item @click="doLogout">
          <LogoutOutlined />
          退出登录
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
</div>
  <div v-else>
    <a-button type="primary" href="/user/login">登录</a-button>
  </div>
</div>



</a-row>
  </div>
</template>

<script lang="ts" setup>
import { h, ref,computed, reactive,onMounted } from 'vue'
import { HomeOutlined,LogoutOutlined } from '@ant-design/icons-vue'
import { MenuProps } from 'ant-design-vue'
import {useRoute,useRouter}  from 'vue-router'
import {useLoginUserStore} from '@/stores/useLoginUserStore'
import{userLogoutUsingPost} from '@/api/userController'
import { message } from 'ant-design-vue'
import { UserOutlined } from '@ant-design/icons-vue';



const loginUserStore = useLoginUserStore()
const router =useRouter()
const current = ref<string[]>(['/'])


const originItems = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: 'others',
    label: h('a', { href: 'https://www.comfred.cn', target: '_blank' }, 'compass'),
    title: 'compass',
  },

  {  
  key: '/add_picture',  
  label: '创建图片',  
  title: '创建图片',  
},
{  
  key: '/admin/pictureManage',  
  label: '图片管理',  
  title: '图片管理',  
},
{
  key: '/admin/spaceManage',
  label: '空间管理',
  title: '空间管理',
},



])



// 定义方法
const doMenuClick=({key}:{key:string})=>{
  router.push({
    path:key,
  })
};

// 后置路由
router.afterEach((to,from,next)=>{
  current.value=[to.path]
})


// 用户注销
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}

// 编写过滤菜单项，根据用户决定展示内容
// 过滤菜单项
const filterMenus = (menus: MenuProps['items'] = []) => {
  // console.log('menus:',menus.value);
  return menus.value.filter((menu) => {
    if (menu.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser;
      if (!loginUser || loginUser?.userRole !== "admin") {
        return false;
      }
    }
    return true;
  });
};

// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))
</script>


<style scoped>
.globalHeader{
  background-color: white;
}

.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>









